
/*
'===============================================================================
'  Generated From - BLL_Component_Partial_Core.vbgen
'  
'  This object is partial class component representing a table in a database. It
'  contains core data access methods. Other partial classes should be used for 
'  implementing custom business logic. This class can be regenerated many times.
'  
'  Do not write custom business logic in this class as it will be overwritten 
'  next time the class is generated.
'
'  Version: 1.0
'  Date:	30/01/06
'  Summary: SelectOne, SelectAll, SelectAllWForeignKey, Insert, Update, Delete,
'  			DeleteAllWForeignKey, class for sorting.
'===============================================================================
*/
using System;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;

namespace PlacementManager
{
    [DataObjectAttribute]
	public partial class tblAgencyComponent
    {
        
		/// <summary>
        /// Default constructor.
        /// </summary>
		public tblAgencyComponent()
        {

        }
		
		/// <summary>
        /// Selects one record from the database table using the primary key.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
		[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
		public tblAgency SelectOne(int AgencyID)
        {  
			PlacementManager.DAL.tblAgency_DAL o = new PlacementManager.DAL.tblAgency_DAL();
			o.AgencyID = AgencyID;
			DataTable dt = o.SelectOne().Tables[0];
			
			if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
				PlacementManager.tblAgency tblAgencyToReturn = new PlacementManager.tblAgency((int)row["AgencyID"], (int)row["ParentAgencyID"], (string)row["AgencyName"], (string)row["StreetAddress"], (string)row["PostalAddress"], (string)row["TownCode"], (string)row["Phone"], (string)row["Mobile"], (string)row["Fax"], (string)row["Email"]);
				return tblAgencyToReturn;
			}
			else
			{
				PlacementManager.tblAgency tblAgencyToReturnEmpty = new PlacementManager.tblAgency();
                return tblAgencyToReturnEmpty;
			}
        }
		
		/// <summary>
        /// Selects all records from the database table.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
		[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
		public List<tblAgency> SelectAll(string sortExpression)
        {
            List<tblAgency> listtblAgency = new List<tblAgency>();
            PlacementManager.DAL.tblAgency_DAL o = new PlacementManager.DAL.tblAgency_DAL();

            DataSet ds = o.SelectAll();

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listtblAgency.Add(new tblAgency((int)row["AgencyID"], (int)row["ParentAgencyID"], (string)row["AgencyName"], (string)row["StreetAddress"], (string)row["PostalAddress"], (string)row["TownCode"], (string)row["Phone"], (string)row["Mobile"], (string)row["Fax"], (string)row["Email"]));
            }
			
			if (sortExpression == string.Empty)
            {
                sortExpression = "DateCreated desc";
            }

            listtblAgency.Sort(new tblAgencyComparer(sortExpression));
            return listtblAgency;
        }
	
		
		
		
	}
	
	public class tblAgencyComparer : IComparer<tblAgency>
    {
        private string _sortColumn;
        private bool _reverse;

        public tblAgencyComparer(string sortExpression)
        {
            _reverse = sortExpression.ToLowerInvariant().EndsWith(" desc");
            if (_reverse)
            {
                _sortColumn = sortExpression.Substring(0, sortExpression.Length - 5);
            }
            else
            {
                _sortColumn = sortExpression;
            }
        }

        public int Compare(tblAgency a, tblAgency b)
        {
            int retVal = 0;
            switch (_sortColumn)
            {
				case "AgencyID":
					retVal = String.Compare(a.AgencyID.ToString(), b.AgencyID.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "ParentAgencyID":
					retVal = String.Compare(a.ParentAgencyID.ToString(), b.ParentAgencyID.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "AgencyName":
					retVal = String.Compare(a.AgencyName.ToString(), b.AgencyName.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "StreetAddress":
					retVal = String.Compare(a.StreetAddress.ToString(), b.StreetAddress.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "PostalAddress":
					retVal = String.Compare(a.PostalAddress.ToString(), b.PostalAddress.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "TownCode":
					retVal = String.Compare(a.TownCode.ToString(), b.TownCode.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "Phone":
					retVal = String.Compare(a.Phone.ToString(), b.Phone.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "Mobile":
					retVal = String.Compare(a.Mobile.ToString(), b.Mobile.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "Fax":
					retVal = String.Compare(a.Fax.ToString(), b.Fax.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;
				case "Email":
					retVal = String.Compare(a.Email.ToString(), b.Email.ToString(), StringComparison.InvariantCultureIgnoreCase);
					break;

            }
            return (retVal * (_reverse ? -1 : 1));
        }
    }
}
